﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ColorClusteringSOM
{
    class Writer
    {
        TextWriter writer = null;

        public void WriteMatrix(String name, Network network, int rows, int cols)
        {
            writer = new StreamWriter(name+".txt");
            String data = "";

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    
                    Neuron n = network[i, j];
                    data = Weights(n);


                    writer.Write(data);
                    data = "";
                }
                writer.Write("\n");
                
            }
        }
        String s = "";

        private String Weights(Neuron neuron)
        {
             s = "";
           
                s += neuron.Weights[0] + ";";
                s += neuron.Weights[1] + ";";
                s += neuron.Weights[2];
            
            //Console.WriteLine(s);
            return (s + " ");
        }

        public static void  WriteFidler(String data,String nameFile)
        {
            TextWriter w = new StreamWriter(nameFile+".txt");
            w.Write(data);
            w.Close();
        }

        /// <summary>
        /// Log pro testovani clusterizace
        /// </summary>
        /// <param name="nameFile"></param>
        /// <param name="logTxt"></param>
        public static void CreateLog(String nameFile, String logTxt)
        {

            String file = "logs\\" + nameFile + ".txt";
            
            StreamWriter log;

            if (!File.Exists(file))
            {
                log = new StreamWriter(file);
            }
            else
            {
                log = File.AppendText(file);
            }

            // Write to the file:
           // log.WriteLine(DateTime.Now);
            log.WriteLine(logTxt);
            log.WriteLine();

            // Close the stream:
            log.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="network"></param>
        /// <param name="networkName"></param>
        public static void WriteTrainedSom(Network network, String networkName)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(networkName+".bin", FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, network);
            stream.Close();
        }

        public static Network ReadTrainedSom(String networkName)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream("som\\"+networkName, FileMode.Open, FileAccess.Read, FileShare.Read);
            Network network = (Network)formatter.Deserialize(stream);
            stream.Close();

            return network;
        }
    }
}
